

clear all
use "[FOLDER]\Panel_Firm_Labv1_temp.dta" 


//Adjust by which lab quantity measure you use
	global MLab = 129
	global FLab = 2
	distinct Ind if u==0
	global Inds = r(ndistinct) - 1
	global Sizes = 4
	global Cities = 42
	distinct year if u==0
	global Years = r(ndistinct) - 1
	global Res = 16
	global NbrOfParam =  $MLab + $FLab + $Cities + $Sizes + $Years + $Inds + $Res
	disp $NbrOfParam

gen RefGr= lab_M_107 
drop lab_M_107 
rename lab_M_1 lab_M_107 


//Function evaluation program
     program nlceslab
                version 14.2
                syntax varlist [if], at(name)
                local log_VA: word 1 of `varlist'
	
	// Retrieve parameters out of at matrix				
				tempvar LabAgg temp_VA
				gen double `LabAgg' = 0 `if'
                gen double `temp_VA' = 0 `if'
				replace `LabAgg' = `LabAgg' + RefGr  `if'

				
				forvalues i=2(1)$MLab {
				tempname delta 
				scalar `delta' = `at'[1,`i']
				replace `LabAgg' = `LabAgg' + `delta' * (lab_M_`i')  `if'
				}
				
				tempname delta 
				scalar `delta' = `at'[1,$MLab +1]
				replace `LabAgg' = `LabAgg' + `delta' * (lab_FNat)  `if'
	
				tempname delta 
				scalar `delta' = `at'[1,$MLab +2]
				replace `LabAgg' = `LabAgg' + `delta' * (lab_FFor)  `if'
	
	
				//Adj index for number of industry dummies
				forvalues i=1(1)$Inds {
				tempname Industry 
				scalar `Industry' = `at'[1,$MLab + $FLab + `i']
				replace `temp_VA' = `temp_VA' + `Industry'*Ind`i' 	`if'			
				}
				//Adj index for number of firm size dummies
				forvalues i=1(1)$Sizes {
				tempname Size 
				scalar `Size' = `at'[1, $MLab + $FLab  + $Inds +`i']
				replace `temp_VA' = `temp_VA' + `Size'*FirmSize_`i' `if'				
				}
				//Adj index for number of City dummies
				forvalues i=1(1)$Cities {
				tempname City 
				scalar `City' = `at'[1, $MLab + $FLab  + $Inds + $Sizes +`i']
				replace `temp_VA' = `temp_VA' + `City'*k_`i' 	`if'			
				}
				//Adj index for number of year dummies
				forvalues i=1(1)$Years {
				tempname Year 
				scalar `Year' = `at'[1, $MLab + $FLab  + $Inds + $Sizes +$Cities +`i']
				replace `temp_VA' = `temp_VA' + `Year'*y_`i' `if'
				}				


                
				// Now fill in dependent variable

                tempname ic thetaL Cap Cap_Sq Cap_Qu TI TI_Sq TI_Qu TICap TICap_Sq TICap_Qu TI_SqCap TI_SqCap_Sq TI_SqCap_Qu TI_QuCap TI_QuCap_Sq TI_QuCap_Qu 
				scalar `ic' = `at'[1,1]
				scalar `thetaL' = `at'[1,$MLab + $FLab + $Inds + $Sizes +$Cities + $Years + 1]
				scalar `Cap' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 2]
				scalar `Cap_Sq' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 3]
				scalar `Cap_Qu' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 4]
				
				scalar `TI' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 5]
				scalar `TI_Sq' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 6]
				scalar `TI_Qu' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 7]
			
				scalar `TICap' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 8]
				scalar `TICap_Sq' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 9]
				scalar `TICap_Qu' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 10]
				scalar `TI_SqCap' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 11]
				scalar `TI_SqCap_Sq' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 12]
				scalar `TI_SqCap_Qu' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 13]
				scalar `TI_QuCap' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 14]
				scalar `TI_QuCap_Sq' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 15]
				scalar `TI_QuCap_Qu' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 16]
				
				
				replace `temp_VA' = `temp_VA' + `ic' + `thetaL' *ln(`LabAgg') +  `Cap'*logCap + `Cap_Sq'*logCap_Sq + `Cap_Qu'*logCap_Qu + `TI'*logGrosInvTot + `TI_Sq'*logGrosInvTot_Sq + `TI_Qu'*logGrosInvTot_Qu  + `TICap'*logGrosInvTot*logCap + `TICap_Sq'*logGrosInvTot*logCap_Sq + `TICap_Qu'*logGrosInvTot*logCap_Qu + `TI_SqCap'*logGrosInvTot_Sq*logCap + `TI_SqCap_Sq'*logGrosInvTot_Sq*logCap_Sq +`TI_SqCap_Qu'*logGrosInvTot_Sq*logCap_Qu +`TI_QuCap'*logGrosInvTot_Qu*logCap + `TI_QuCap_Sq'*logGrosInvTot_Qu*logCap_Sq + `TI_QuCap_Qu'*logGrosInvTot_Qu*logCap_Qu  `if'
				replace `log_VA' = `temp_VA'   `if'
            end
			
			
cd "[Output Folder]\"

			
mat M = J(1,$NbrOfParam,1)
//set trace on
nl ceslab @ logFtg_Foradlingsvarde if u==0, nparameters($NbrOfParam) initial(M)

parmest, format(estimate min95 max95 %8.2f p %8.1e) saving(HC_v1, replace) omit empty 

use "[Output Folder]\HC_v1.dta", clear

drop parm
gen par_nr= regexr(eq,"b","")
destring par_nr, replace
replace par_nr = 0 if par_nr ==1
replace par_nr = 1 if par_nr ==107
replace par_nr = 300 if par_nr ==130
replace par_nr = 301 if par_nr ==131
keep if par_nr<130 

rename par_nr FodelseLandnamn

rename estimate HC
drop if stderr==0
keep  HC  FodelseLandnamn
save "[Output Folder]\HC_v1_clean.dta"



merge 1:1 FodelseLandnamn using "[Folder]/MacroData_ToMona_Fin.dta"
drop _merge


gen CG=0
replace CG=1 if FodelseLandnamn<8   | (FodelseLandnamn>13 & FodelseLandnamn<19 )   | (FodelseLandnamn>80 & FodelseLandnamn<84 )  | FodelseLandnamn==57 


//Dev. accounting, Handbook data
scalar s_r1=0.134
scalar s_r2=0.101
scalar s_r3=0.068

gen hc_hb=.
replace hc_hb = s_r1*ysch_hb if ysch_hb<=4
replace hc_hb = s_r1*4 + s_r2*(ysch_hb-4) if ysch_hb>4 & ysch_hb<=8
replace hc_hb = s_r1*4 + s_r2*4 + s_r3*(ysch_hb-8) if ysch_hb>8
replace hc_hb=exp(hc_hb)

gen h_adj=hc_hb*HC_v1 

gen ln_h=ln(hc_hb)
gen ln_y=ln(y_hb)
gen ln_h_adj=ln(h_adj)


sum ln_y if !missing(ln_h_adj) & !missing(y_hb) & CG==0
scalar var_lny = r(Var)

sum ln_h if !missing(ln_h_adj) & !missing(y_hb)  & CG==0
scalar var_ln_h = r(Var)

sum ln_h_adj if !missing(ln_h_adj) & !missing(y_hb)  & CG==0
scalar var_ln_h_adj = r(Var)

//Data points for the two righmost numbers in Table 3:
disp var_ln_h/var_lny
disp var_ln_h_adj/var_lny



//Dev. accounting, PWT10, 2014 data:
drop hc_hb h_adj ln_h ln_y ln_h_adj


gen ln_y=ln(y2014)
gen ln_k=ln(k2014)
gen ln_h=ln(hc2014)

gen h_adj=hc2014*HC_v1 
gen ln_h_adj=ln(h_adj)

sum ln_y if !missing(ln_h_adj) & !missing(y2014) & CG==0
scalar var_lny = r(Var)

sum ln_h if !missing(ln_h_adj) & !missing(y2014)  & CG==0
scalar var_ln_h = r(Var)

sum ln_h_adj if !missing(ln_h_adj) & !missing(y2014)  & CG==0
scalar var_ln_h_adj = r(Var)

//Data points corresponding to the two righmost numbers in Table 3:
disp var_ln_h/var_lny
disp var_ln_h_adj/var_lny

//90/10-ratio for measure of human capital:
sum HC_v1 if !missing(loggdp05) & CG==0 , d
disp r(p90)/r(p10)


//Table 4 Results: 
merge 1:1 FodelseLandnamn using  "[Output Folder]/Weights.dta"

gen Y003_Norm=Y003/0.514
gen A165_Norm = (2-A165)/0.166

gen Weights=Weights_Tot

reg HC_v1 TR SS cognitive FertRate LifeExp  [aw=Weights] if WVInd==1 & !missing(cognitive)  , vce(cluster FodelseLandnamn)
reg HC_v1 TR SS cognitive FertRate LifeExp loggdp05 [aw=Weights] if WVInd==1 & !missing(cognitive)  , vce(cluster FodelseLandnamn) 

reg HC_v1 Y003_Norm A165_Norm cognitive FertRate LifeExp  [aw=Weights] if WVInd==1 & !missing(cognitive)  & !missing(TR), vce(cluster FodelseLandnamn) 
reg HC_v1 Y003_Norm A165_Norm cognitive FertRate LifeExp loggdp05 [aw=Weights] if WVInd==1 & !missing(cognitive)  & !missing(TR), vce(cluster FodelseLandnamn) 
 
